package com.degon.hsyl.videobannerdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.listener.OnPageChangeListener;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private MyBannerAdapter adapter;
    private List<MyBannerAdapter.BannerListBean> urlList = new ArrayList<>();
    private Banner banner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        banner = findViewById(R.id.banner);

        MyBannerAdapter.BannerListBean bean1 = new MyBannerAdapter.BannerListBean();
        bean1.setUrl("https://stream7.iqilu.com/10339/upload_transcode/202002/18/20200218114723HDu3hhxqIT.mp4");
        bean1.setVedio(true);
        urlList.add(bean1);
        MyBannerAdapter.BannerListBean bean2 = new MyBannerAdapter.BannerListBean();
        bean2.setUrl("https://cdn.juliye.net/Fr6SULz4_nTmn8jgXPhpCSCrK6Oh");
        bean2.setVedio(false);
        urlList.add(bean2);

        initBanner();
    }

    private void initBanner() {
        adapter = new MyBannerAdapter(this, urlList);
        banner.addBannerLifecycleObserver(this)
                .setStartPosition(0)
                .setAdapter(adapter, false)
                .isAutoLoop(false)
                .setIndicator(new CircleIndicator(this));
        banner.addOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //这里是只又一个视频的情况，如果滑动后不是第一个，释放掉视频
                if (position != 0) {
                    adapter.setVideoStop();
                }
                //如果滑动还有页数角标的需求，还可以在这边更新角标
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(adapter != null){
            adapter.setVideoStop();
        }
    }
}